package com.ewzj.bigdata.mvp.presenter;

import android.app.Application;
import android.content.Context;

import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseJson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.RegisterContract;
import com.jess.arms.utils.ArmsUtils;


@ActivityScope
public class RegisterPresenter extends BasePresenter<RegisterContract.Model, RegisterContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;

    @Inject
    public RegisterPresenter(RegisterContract.Model model, RegisterContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void sendSms(String phone) {
        mModel.sendSms(phone).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<String>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<String> baseJson) {
                        if (baseJson.isSuccess()){
                            //  发送成功
                            ArmsUtils.snackbarText("验证码发送成功");
                        }
                    }
                });
    }

    public void register(String phone, String pass, String code) {
        mModel.register(phone,pass,code).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<String>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<String> stringBaseData) {
                        if (stringBaseData.isSuccess()){
                            ArmsUtils.snackbarText("注册成功");
                        }
                    }
                });
    }
}
